<?php
    class AdminNews extends iJBlock
    {
        /**
         *
         * This function is construct Languages
         *
         *
         * @access public
         */
        public function __construct() {
        }
        
        /**
         * execute
         *
         * Executes the block to generate the tokens
         *
         * @return array Array with the tokens of the block
         *
         * @access public
         */
        public function execute() {
          $iJDataBase =  iJFactory::singleton('iJDataBase');
          
          
          if(isset($_POST['text'])) {
            if(!empty($_POST['text']) && !empty($_POST['title'])) {
                $QNews = $iJDataBase->query('INSERT INTO :table_news (text, title, date)
                                            VALUES(
                                                     :text, 
                                                     :title, 
                                                     NOW()
                                                   )
                                        ON DUPLICATE KEY UPDATE 
                                            text = :text, 
                                            date = NOW()');
                                          
                $QNews->bindTable(':table_news', TABLE_NEWS);
                $QNews->bindValue(':text',  mysql_real_escape_string($_POST['text']));
                $QNews->bindValue(':title', $_POST['title']);
                $QNews->execute();
            }
            
            

          }
          if(isset($_GET['newsID'])) {
            $QNews = $iJDataBase->query('SELECT  title, text
                                        FROM :table_news 
                                        WHERE id = :id
                                        ORDER BY date');
            $QNews->bindTable(':table_news', TABLE_NEWS);
            $QNews->bindValue(':id', $_GET['newsID']);
            $QNews->execute();
            if ($QNews->next()) {
              $this->addToken('title', $QNews->value('title'));
              $this->addToken('text',  $QNews->value('text'));
              $this->addToken('delete_link', iJHrefLink::linkToLocalPage('admin_news'). '&delID=' . $_GET['newsID']);
            }

          }
          
          if(isset($_GET['delID']))  {
            $QNews = $iJDataBase->query('DELETE FROM :table_news 
                                        WHERE id = :id');
            $QNews->bindTable(':table_news', TABLE_NEWS);
            $QNews->bindValue(':id', $_GET['delID']);
            $QNews->execute();
          }
          
          $QNews = $iJDataBase->query('SELECT id, title
                                      FROM :table_news ORDER BY DATE DESC  ');
          $QNews->bindTable(':table_news', TABLE_NEWS);
          $QNews->execute();
          $news = array();
          while ($QNews->next()) {
            $news[$QNews->value('id')]['title'] = $QNews->value('title');
            $news[$QNews->value('id')]['href']  = iJHrefLink::linkToLocalPage('admin_news') . '&newsID=' . $QNews->value('id');
            
          }
          $this->addToken('news', $news);
          $this->addToken('form_action', iJHrefLink::linkToLocalPage('admin_news'));
          
          
          
          return parent::execute();
        }
        
    }
?>